#include "volumetool.h"
#include "ui_volumetool.h"

#include <QGraphicsDropShadowEffect>
#include <QPainter>
#include <QDebug>

VolumeTool::VolumeTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VolumeTool),
    isMuted(false),
    volumeRatio(20)
{
    ui->setupUi(this);

    //需要设置为弹出窗口，无阴影，设置透明效果如果不加 Qt::FramelessWindowHint 背景为黑色
    setWindowFlags(Qt::Popup | Qt::NoDropShadowWindowHint | Qt::FramelessWindowHint);

    //设置背景为透明
    setAttribute(Qt::WA_TranslucentBackground);

    //设置 volumeTool 阴影效果
    QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setOffset(0, 0);
    shadowEffect->setColor("#646464");
    shadowEffect->setBlurRadius(10);
    this->setGraphicsEffect(shadowEffect);

    //设置按钮图标
    ui->silenceBtn->setIcon(QIcon(":/images/volume.png"));

    //音量的默认大小为 20%
    ui->volumeRatio->setText("20%");
    ui->outLine->setGeometry(ui->outLine->x(), ui->outLine->y() + 180 - 36, ui->outLine->width(), 36);
    ui->sliderBtn->move(ui->sliderBtn->x(), ui->sliderBtn->y() + 180 - 36);

    //音量相关信号与槽函数
    connect(ui->silenceBtn, &QPushButton::clicked, this, &VolumeTool::onSilenceBtnClicked);

    //设置时间过滤器
    ui->volumeBox->installEventFilter(this);
}

VolumeTool::~VolumeTool()
{
    delete ui;
}

void VolumeTool::onSilenceBtnClicked()
{
    isMuted = !isMuted;
    if(isMuted)
    {
        ui->silenceBtn->setIcon(QIcon(":/images/silence.png"));
    }
    else ui->silenceBtn->setIcon(QIcon(":/images/volume.png"));

    emit setSilence(isMuted);
}

void VolumeTool::renewMusicVolume()
{
    //计算当前光标相对于 ui->volumeBox 中的位置
    int height = ui->volumeBox->mapFromGlobal(QCursor().pos()).y();

    //验证 height 的合法性
    height = height < MIN_VALUE_HEIGHT ? MIN_VALUE_HEIGHT : height;
    height = height > MAX_VALUE_HEIGHT ? MAX_VALUE_HEIGHT : height;

    //重新调整 outLine 的长度
    ui->outLine->setGeometry(ui->outLine->x(), height, ui->outLine->width(), MAX_VALUE_HEIGHT - height);

    //重新调整 sliderBtn 的位置
    ui->sliderBtn->move(ui->sliderBtn->x(), height - ui->sliderBtn->height() / 2);

    //计算音量大小
    volumeRatio = (int)((MAX_VALUE_HEIGHT - height) * 100.0 / 180);

    //显示音量大小
    ui->volumeRatio->setText(QString().number(volumeRatio) + "%");
}

void VolumeTool::paintEvent(QPaintEvent *event)
{
    (void) event;

    //创建绘图对象
    QPainter painter(this);

    //设置画笔的样式和画刷的颜色
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::white);

    //创建一个三角形
    QPolygon polygon;
    QPoint a(30, 320);
    QPoint b(70, 320);
    QPoint c(50, 335);
    polygon.append(a);
    polygon.append(b);
    polygon.append(c);

   //绘制这个三角形
    painter.drawPolygon(polygon);
}

bool VolumeTool::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->volumeBox)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
            //鼠标按下事件
            renewMusicVolume();
        }
        else if(event->type() == QEvent::MouseButtonRelease)
        {
            //鼠标释放事件
            emit renewVolume(volumeRatio);
        }
        else if(event->type() == QEvent::MouseMove)
        {
            //鼠标移动事件
            renewMusicVolume();
            emit renewVolume(volumeRatio);
        }
        return true;
    }
    return QObject::eventFilter(watched, event);
}
